package com.mingqijia.gassafety.job.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * SID赋值
 *
 */
@Aspect
@Component
public class AspectjSid {

    @Before("execution(* com.mingqijia.gassafety.job.job.*..*.*(..))" +
            " or execution(* com.mingqijia.gassafety.job.job..*.*(..))") // 拦截被TestAnnotation注解的方法；如果你需要拦截指定package指定规则名称的方法，可以使用表达式execution(...)，具体百度一下资料一大堆
    public void beforeTest(JoinPoint point) {
        String sessionId = UUID.randomUUID().toString().replace("-", "");
        MDC.put("trace_id", sessionId);
    }

    @After("execution(* com.mingqijia.gassafety.job.job.*..*.*(..))" +
            " or execution(* com.mingqijia.gassafety.job.job..*.*(..))")
    public void afterTest(JoinPoint point) {
        MDC.remove("trace_id");
    }
}
